<template>
  <div>
    <a-button :disabled="selectedRowKeys.length === 0" type="primary" :loading="btnLoading" class="float-right mb-2" @click="onApplyBatch()">
      批量申请
    </a-button>
    <BaseTable
      fill
      row-key="id"
      :scroll="{ x: 2000 }"
      column-code="00001123"
      :loading="!!store.loading"
      :data-source="store.todaySignData"
      :row-selection="{
        selectedRowKeys,
        onChange: onSelectChange
      }"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'signNurseStatusName'">
          <a-tag color="blue">{{ record.signNurseStatusName }}</a-tag>
        </template>
        <template v-if="column.dataIndex === 'advExecTypeName'">
          {{ record.advExecTypeName }}
        </template>
        <template v-if="column.dataIndex === 'children'">
          <template v-if="record.advOrderType === 0">
            <DoctorAdviceOrder
              :row-data="record"
              :params-key="{
                mc: 'adv.mc',
                gg: 'adv.gg',
                sl: 'clinicalDose',
                dw: 'clinicalDoseUnit',
                yf: 'deliverRoute.fyff',
                pl: 'fqy.plid',
                drugPathName: 'drugPathName'
              }"
            />
          </template>
          <template v-else>
            <div class="pl-2">
              <!-- {{ record?.advName }} -->
              <a-tooltip>
                <template #title>{{ record?.advName }}</template>
                {{ record?.advName }}
              </a-tooltip>
            </div>
          </template>
          <!-- <DoctorAdviceOrder
            :row-data="record"
            :params-key="{ mc: 'adv.mc', gg: 'adv.gg', sl: 'clinicalDose', dw: 'clinicalDoseUnit', yf: 'deliverRoute.fyff', pl: 'fqy.pl' }"
          /> -->
        </template>
        <template v-if="column.dataIndex === 'action'">
          <BaseButton type="link" size="small" @click="onApply([record.id])"> 申请 </BaseButton>
        </template>
      </template>
    </BaseTable>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import BaseTable from '@/components/BaseTable'
import BaseButton from '@/components/BaseButton'
import useStore from '../store'

const store = useStore()
const btnLoading = ref(false)
const selectedRowKeys = ref([])
watch(
  () => store.todaySignData,
  () => {
    selectedRowKeys.value = []
  },
  { immediate: true }
)
const onSelectChange = (keys) => {
  selectedRowKeys.value = keys
}
// 申请
const onApply = async (yzbhs) => {
  const res = await post('/inp/clinicalOrder/doApplyClinicalOrder', { yzbhs })
  btnLoading.value = false
  res.code === 0 ? message.success(res.msg) : message.error(res.msg || '操作失败')
  store.fetchSignData()
  store.fetchSignCountData()
  store.fetchAllOrderCountData()
}
// 批量申请
const onApplyBatch = () => {
  selectedRowKeys.value.filter((item) => !!item)
  const rowId = store.todaySignData.map((item) => item.id)
  const resultArr = selectedRowKeys.value.filter((item) => rowId.includes(item))
  btnLoading.value = true
  onApply(resultArr)
}

// 取消申请
// const onCancelApply = async ({ id }) => {
//   const res = await post('/inp/clinicalOrder/doCancelApplyClinicalOrder', { id })
//   res.code === 0 ? message.success(res.msg) : message.error(res.msg || '操作失败')
//   store.fetchSignData()
// }
</script>
